package com.authManager.server.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.authManager.entity.SysMenu;
import com.authManager.server.dao.SysMenuMapper;
import com.authManager.server.service.ISysMenuService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.commons.base.model.AdminPage;
import com.commons.base.model.ResultCode;
import com.commons.base.model.ResultData;

/**
 * <p>
 * 【菜单信息】 服务实现类
 * </p>
 *
 * @author 小旋风
 * @since 2018-8-17
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(SysMenuServiceImpl.class);

	@Override
	public ResultData<AdminPage<SysMenu>> listSysMenu(Map<String, Object> paramMap) {
		Page<SysMenu> page = new Page<>(Integer.valueOf(String.valueOf(paramMap.get("pageNum"))),
				Integer.valueOf(String.valueOf(paramMap.get("pageSize"))));
		Wrapper<SysMenu> entityWrapper = new EntityWrapper<>();

		// 主键
		if (paramMap.get("sysMenuId") != null) {
			entityWrapper.where("FIND_IN_SET(sys_menu.sys_menu_id ,(SELECT queryChildrenMenuId("
					+ paramMap.get("sysMenuId") + "))) ");
		}
		// 资源名称
		if (paramMap.get("name") != null) {
			entityWrapper.like("sys_menu.name", String.valueOf(paramMap.get("name")));
		}
		// menu_type等于0 的唯一 code
		if (paramMap.get("systemCode") != null) {
			entityWrapper.like("sys_menu.system_code", String.valueOf(paramMap.get("systemCode")));
		}
		// 资源路径
		if (paramMap.get("pageUrl") != null) {
			entityWrapper.like("sys_menu.page_url", String.valueOf(paramMap.get("pageUrl")));
		}
		// 资源路径 跳转方式
		if (paramMap.get("pageUrlType") != null) {
			entityWrapper.like("sys_menu.page_url_type", String.valueOf(paramMap.get("pageUrlType")));
		}
		// 父级资源id
		if (paramMap.get("parentId") != null) {
			entityWrapper.eq("sys_menu.parent_id", paramMap.get("parentId"));
		}
		// 菜单所在位置（left rigit top）
		if (paramMap.get("site") != null) {
			entityWrapper.like("sys_menu.site", String.valueOf(paramMap.get("site")));
		}
		// 排序
		if (paramMap.get("sortNum") != null) {
			entityWrapper.eq("sys_menu.sort_num", paramMap.get("sortNum"));
		}
		// 资源图标
		if (paramMap.get("icon") != null) {
			entityWrapper.like("sys_menu.icon", String.valueOf(paramMap.get("icon")));
		}
		// 资源类别 0系统 1菜单 2按钮
		if (paramMap.get("menuType") != null) {
			entityWrapper.eq("sys_menu.menu_type", paramMap.get("menuType"));
		}
		// 资源介绍
		if (paramMap.get("description") != null) {
			entityWrapper.like("sys_menu.description", String.valueOf(paramMap.get("description")));
		}
		// 按钮标签【查询query 列表查询queryList 增加save 编辑edit 删除 del】
		if (paramMap.get("buttonMark") != null) {
			entityWrapper.like("sys_menu.button_mark", String.valueOf(paramMap.get("buttonMark")));
		}
		//
		if (paramMap.get("field1") != null) {
			entityWrapper.like("sys_menu.field1", String.valueOf(paramMap.get("field1")));
		}
		//
		if (paramMap.get("field2") != null) {
			entityWrapper.like("sys_menu.field2", String.valueOf(paramMap.get("field2")));
		}
		//
		if (paramMap.get("field3") != null) {
			entityWrapper.like("sys_menu.field3", String.valueOf(paramMap.get("field3")));
		}
		// 开始-创建时间
		if (paramMap.get("start_createTime") != null) {
			entityWrapper.le("sys_menu.create_time", paramMap.get("start_createTime"));
		}
		// 结束-创建时间
		if (paramMap.get("end_createTime") != null) {
			entityWrapper.le("sys_menu.create_time", paramMap.get("end_createTime"));
		}
		this.selectPage(page, entityWrapper.orderBy("sys_menu.sort_num desc,sys_menu.sys_menu_id asc"));
		return new ResultData.Builder<AdminPage<SysMenu>>(ResultCode.success).setData(new AdminPage<SysMenu>(page))
				.build();
	}
	
	@Override
	public ResultData<List<SysMenu>> listSysMenuByRoleAndUserId(String systemCode, String sysUserId,String menuSite) {
		
		Wrapper<SysMenu> entityWrapperBySystemCode = new EntityWrapper<>(
				new SysMenu().setSystemCode(systemCode).setMenuType(0));
		SysMenu sysMenu = this.selectOne(entityWrapperBySystemCode);
		if (sysMenu == null) {
			return new ResultData.Builder<List<SysMenu>>(ResultCode.isNull)
					.setMessage("根据 系统code:【" + systemCode + "】查无此系统").build();
		}
		if(StringUtils.isBlank(menuSite)) {
			menuSite = null;
		}
		if(StringUtils.isNotBlank(menuSite) && "all".equalsIgnoreCase(menuSite)) {
			menuSite = null;
		}
		
		List<SysMenu> list = baseMapper.listSysMenuByRoleAndUserId(systemCode, sysUserId,menuSite);
		return new ResultData.Builder<List<SysMenu>>(ResultCode.success).setData(list).build();
	}
}
